from typing import Any
from bcat.app.application import ServerApplication, IApplication
from bcat.rpc.server.server import IServer
class CommonServerApplication(ServerApplication):
    def __init__(self, service: Any, server: IServer):
        self.__service = service
        self.__server = server
        self.init()

    def __del__(self):
        self.deinit()

    def init(self) -> bool:
        #TODO: function tick check ???? && register failed check
        self.__server.register("tick", self.__service.tick)
        self.__server.register("exit", self.__service.exit)
        return True

    def deinit(self) -> bool:
        #TODO: unregister check
        self.__server.unregister("exit")
        self.__server.unregister("tick")
        self.__server = None
        self.__service = None
        return True

    def run(self) -> IApplication:
        self.__server.run()
        return self


    def stop(self) -> bool:
        self.__service.exit()
        return True


    def __repr__(self):
        return f"CommonServerApplication(server:{{TODO:self.__server}}, service:{{TODO:self.__service}}"
